package com.springboot.module.user.controller.user;

import com.springboot.module.user.controller.user.vo.UserImportRespVO;
import com.springboot.module.user.controller.user.vo.UserReqVo;
import com.springboot.module.user.controller.user.vo.UserResVo;
import com.springboot.module.user.controller.user.vo.UserSaveReqVo;
import com.springboot.module.user.service.user.UserService;
import com.springboot.module.user.utils.result.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //用户登录
    @PostMapping("login")
    public ApiResult<UserResVo> userLogin(@RequestBody UserReqVo userReqVo){
        UserResVo userResVo = userService.login(userReqVo);
        if(userResVo != null){
            return ApiResult.success(userResVo);
        }
        return ApiResult.failure("登录失败");
    }

    //查询所有用户
    @GetMapping("all")
    public ApiResult<List> selectAll(){
        List<UserResVo> userResVos = userService.selectAll();
        return ApiResult.success(userResVos);
    }

    //根据id查询用户
    @GetMapping("/{id}")
    public ApiResult<UserResVo> selectOne(@PathVariable("id") Long id){
        UserResVo userResVo = userService.getById(id);
        return ApiResult.success(userResVo);
    }

    //分页查询
    @GetMapping("/page")
    public ApiResult<List> pageUser(@RequestBody UserReqVo userReqVo){
        List pageUser = userService.page(userReqVo);
        return ApiResult.success(pageUser);
    }
    //条件分页查询

    @GetMapping
    public ApiResult<List> page(@RequestBody UserReqVo userReqVo) {
        log.info("查询请求参数： {}, {}, {}, {}, {}, {}", userReqVo.getPageNumber(),userReqVo.getPageSize(),userReqVo.getUsername(),userReqVo.getPhone(),userReqVo.getBegin(),userReqVo.getEnd());
        userService.likePage(userReqVo);
        return ApiResult.success();
    }
    //新增用户
    @PostMapping("/save")
    public ApiResult<Long> saveUser(@RequestBody UserSaveReqVo userSaveReqVo){
        Long userId = userService.saveUser(userSaveReqVo);
        return ApiResult.success(userId);
    }
    //根据ID删除某个用户
    @DeleteMapping("/{id}")
    public ApiResult<Void> deleteUser(@PathVariable("id") Long id){
        userService.deleteById(id);
        return ApiResult.success();
    }
    //批量删除用户
    @DeleteMapping("/batch")
    public ApiResult<Void> deleteUsers(@RequestBody Set<Long> ids){
        userService.deleteBatch(ids);
        return ApiResult.success();
    }
    //修改用户
    @PutMapping("/updata")
    public ApiResult<UserSaveReqVo> updateUser(@RequestBody UserSaveReqVo userSaveReqVo){
        userService.updateUser(userSaveReqVo);
        return ApiResult.success();
    }
    //导入excel表
    @GetMapping("/export-excel")
    public ApiResult exportUserList(){
        userService.exportExcel();
        return ApiResult.success();
    }
    //从excel导入数据库
    //拿到excel数据->遍历数据->判断数据是否合规->判断数据是否在数据库中是否存在
    @PostMapping("/import")
    public ApiResult<UserImportRespVO> importExcel(@RequestParam("file") MultipartFile file) throws Exception {

        return ApiResult.success();
    }
}
